<?php
/**
 * Created by PhpStorm.
 * User: LOPO
 * Date: 2020/4/17
 * Time: 11:39
 */
namespace App\Model;
use EasySwoole\Component\Di;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\Mysqli\Config;
use EasySwoole\Mysqli\Client;
use EasySwoole\EasySwoole\Config as Configs;
class Base{
    public $db = "";
    public function __construct() {
        if(empty($this->tableName)) {
            throw new \Exception("table error");
        }

        $config=Configs::getInstance()->getConf('MYSQL');
        $conf = new Config($config);
        $db = new Client($conf);
        if($db instanceof Client) {
            $this->db = $db;
        } else {
            throw new \Exception("db error");
        }
    }
    /**
     * [add description]
     * @auth  singwa
     * @date  2018-10-21T16:38:42+0800
     * @param [type]                   $data [description]
     */
    public function add($data) {
        if(empty($data) || !is_array($data)) {
            return false;
        }
        $this->db->queryBuilder()->insert($this->tableName, $data);
        $data1 = $this->db->execBuilder();
        return $data1;
    }

    /**
     * 通过ID 获取 基本信息
     *
     * @param [type] $id
     * @return void
     */
    public function getById($id) {
        $id = intval($id);
        if(empty($id)) {
            return [];
        }

        $this->db->where ("id", $id);
        $result = $this->db->queryBuilder()->getOne($this->tableName);
        return $result ?? [];
    }
}